﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;

namespace IronCow.Posh.Commands
{
    [Cmdlet(VerbsCommon.Get, "RtmAuthenticationToken")]
    [System.ComponentModel.Description("Gets the authentication token from RTM.")]
    public class GetRtmAuthenticationTokenCommand : RtmCmdlet
    {
        [Parameter(
            Mandatory = true,
            Position = 1,
            ValueFromPipeline = true,
            HelpMessage = "The frob value to use for getting the authentication token.")]
        [ValidateNotNullOrEmpty]
        public string Frob { get; set; }

        [Parameter(
            HelpMessage = "Whether to save the authentication token in the user settings.")]
        [Alias("Save")]
        public SwitchParameter SaveUserSettings { get; set; }

        public GetRtmAuthenticationTokenCommand()
        {
            SaveUserSettings = true;
        }

        protected override void ProcessRecord()
        {
            WriteVerbose("Getting token from RTM...");
            string token = Rtm.GetToken(Frob);
            if (SaveUserSettings)
            {
                WriteVerbose("Got token. Saving user settings...");
                Properties.Settings.Default.Token = token;
                Properties.Settings.Default.Save();
            }
            Rtm.AuthToken = token;
            
            WriteObject(token);
        }
    }
}
